// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// h [author] rhyme_lph
// P [email]  rhymelph@gmail.com
// w [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';
import 'package:finance_app/src/http_service/http_preference_service.dart';
import 'package:finance_app/src/widgets/bottom_sheet/got_it_tip_bottom_sheet.dart';
import 'package:finance_app/src/widgets/bottom_sheet/judge_bottom_sheet.dart';
import 'package:finance_app/src/widgets/bottom_sheet/one_input_bottom_sheet.dart';

class UnitOfMeasurementProvider extends BaseProvider {
  int newTextId;

  @override
  Future get dataFuture => null;

  void onAddUom() {
    obtainContext?.call((context) async {
      final result = await showAddUOMInputBottomSheet(context);
      if (result != null && result is String) {
        //添加单位
        List<SettingDataUnitType> entity;
        await post(HttpPreferenceService.addUnitTypes(result),
            autoHandler: AutoHandler.Toast,
            loadingTip: S.current.loading, onSuccess: (result) {
          entity = getEntityFromResult<List<SettingDataUnitType>>(result);
        });
        if (entity != null) {
          newTextId = entity
              .lastWhere((element) => element.name == result,
                  orElse: () => null)
              ?.id;
          Config.settingInfo.unitTypes = entity;
          Config.settingData.unitType = entity;
          await RProvider.myProvider.updateGlobalMap();
        }
      }
    });
  }

  void onSelect(int index) {
    final select = Config.settingInfo.unitTypes[index];
    obtainContext?.call((context) async {
      final result =
          await showAddUOMInputBottomSheet(context, name: select.name);
      if (result != null && result is bool) {
        //删除对应的单位
        bool isSuccess = await showDeleteUOMJudgeBottomSheet(context);
        if (isSuccess == true) {
          List<SettingDataUnitType> entities;
          await post(
              HttpPreferenceService.deleteUnitTypes(
                select.name,
              ),
              autoHandler: AutoHandler.Toast, onSuccess: (result) {
            entities = getEntityFromResult<List<SettingDataUnitType>>(result);

            isSuccess = true;
          }, loadingTip: S.current.loading);
          if (entities == null) return null;
          Config.settingInfo.unitTypes = entities;
          Config.settingData.unitType = entities;
          await RProvider.myProvider.updateGlobalMap();
          obtainContext?.call((context) {
            showDeleteUnitTypeSuccessTipBottomSheet(context);
          });
        }
      } else if (result != null && result is String) {
        //更改单位
        List<SettingDataUnitType> entities;
        await post(
            HttpPreferenceService.deleteUnitTypes(
              select.name,
            ),
            autoHandler: AutoHandler.Toast, onSuccess: (result) {
          entities = getEntityFromResult<List<SettingDataUnitType>>(result);
        }, loadingTip: S.current.loading);
        if (entities == null) return null;
        Config.settingInfo.unitTypes = entities;
        Config.settingData.unitType = entities;
        await RProvider.myProvider.updateGlobalMap();
        await post(
            HttpPreferenceService.addUnitTypes(
              result,
            ),
            autoHandler: AutoHandler.Toast, onSuccess: (result) {
          entities = getEntityFromResult<List<SettingDataUnitType>>(result);
        }, loadingTip: S.current.loading);
        if (entities == null) return null;
        Config.settingInfo.unitTypes = entities;
        Config.settingData.unitType = entities;
        await RProvider.myProvider.updateGlobalMap();
        update();
      }
    });
  }
}
